//
//  ImageController.m
//  MY Project For Sport
//
//  Created by dllo on 16/2/25.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "ImageController.h"
#import "NetworkingHandlerOfBlock.h"
#import "ModelForImage.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "ModelForTitle.h"
#import "PunchedLayout.h"
#import "CollectionCellForImage.h"
#import <UMSocial.h>

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface ImageController ()<UIScrollViewDelegate,UICollectionViewDataSource, UICollectionViewDelegate,UMSocialUIDelegate>


@property (nonatomic, retain)ModelForTitle *model;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)UIButton *last;
@property (nonatomic, retain)UIButton *share;
@property (nonatomic, retain)UICollectionView *collection;
@property (nonatomic, retain)NSMutableArray *arrayForTitle;

@end

@implementation ImageController

-(void)dealloc {
    
    [_arrayForTitle release];
    [_model release];
    [_array release];
    [_last release];
    [_share release];
    [_collection release];
    [super dealloc];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBarHidden = YES;
    // NSLog(@"%@", self.url);
    [self createCollection];
    [self handleData];
    [self createTwoButton];
    
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"share" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
       
        
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:nil
                                          shareText:@"这个 app 太牛了"
                                         shareImage:[UIImage imageNamed: [note.userInfo objectForKey:@"image"]]
                                    shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToQzone,UMShareToQQ,nil]
                                           delegate:self];
    }];

    
}
/** 创建两个 button (分享,返回上一页)*/
- (void)createTwoButton {
    
    self.last = [UIButton buttonWithType:UIButtonTypeCustom];
    self.last.frame = CGRectMake(5 * WEIGHTT, 20 * HEIGHTT , WIDTH / 9, 44);
    [self.view addSubview:self.last];
    // self.last.backgroundColor = [UIColor whiteColor];
    [self.last setImage:[UIImage imageNamed:@"iconfont-zuojiantou"] forState:UIControlStateNormal];
    [self.last addTarget:self action:@selector(lastAction:) forControlEvents:UIControlEventTouchUpInside];
    
    

}

- (void)createCollection {
    
    PunchedLayout *flayout = [[PunchedLayout alloc] init];
    //    UICollectionViewFlowLayout *flayout = [[UICollectionViewFlowLayout alloc] init];
    flayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flayout.itemSize = CGSizeMake(WIDTH, HEIGHT);
    flayout.minimumInteritemSpacing = 0;
    flayout.minimumInteritemSpacing = 0;
    // flayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flayout];
    [flayout release];
    [self.view addSubview:self.collection];
    [_collection release];
    
    self.collection.dataSource = self;
    self.collection.delegate = self;
    //self.collection.pagingEnabled = YES;
    
    [self.collection registerClass:[CollectionCellForImage class] forCellWithReuseIdentifier:@"poolForCollectionImage"];
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.array.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CollectionCellForImage *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCollectionImage" forIndexPath:indexPath];
    
    [cell passModelForImage:[self.array objectAtIndex:indexPath.item]];
    
    if (indexPath.item == 0) {
        cell.labelForName.text = self.model.name ;
        
    } else {
        
        cell.labelForName.text = @"";
        
    }
    
    cell.labelForIntro.text = self.model.intro;
    cell.labelForCount.text =[NSString stringWithFormat:@"%@%ld",@"/ ", self.array.count];
    cell.labelForCurren.text = [NSString stringWithFormat:@"%ld", indexPath.item + 1];
    
      
    return cell;
    
}




- (void)lastAction:(UIButton *) button {
    
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    
}

-(BOOL)isDirectShareInIconActionSheet
{
    return YES;
}


- (void)handleData {
    
    self.array = [NSMutableArray array];
    
    [NetworkingHandler networkingWithUrl:self.url completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
         NSLog(@"%@", self.url);
        NSDictionary *dicd = [result objectForKey:@"album"];
        self.model = [[ModelForTitle alloc] init];
        
        [self.model setValuesForKeysWithDictionary:dicd];
        
        NSDictionary *dic = [result objectForKey:@"data"];
        NSArray *array = [dic objectForKey:@"item"];
        for (NSDictionary *dict in array) {
            
            ModelForImage *model = [[ModelForImage alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            
            [self.array addObject:model];
            
            [model release];
            
        }
        [self.collection reloadData];
        
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
